/**
  ******************************************************************************
  * @file    User/user.c
  * @author  JUSHENG Application Team
  * @version V1.0.4
  * @date    12-14-2021
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "user.h"
#include "tk_cfg.h"

void wk_up_io_fun_set(void)
{
   TYPE_LL_GPIO_INIT        gpio_struct;
   memset(&gpio_struct, 0x0, sizeof(gpio_struct));
   gpio_struct.gpio_pin   =  LL_GPIO_PIN_12;
   gpio_struct.gpio_mode  = LL_GPIO_MODE_AF;
   gpio_struct.gpio_pupd = LL_GPIO_PUPD_DOWN;
   ll_gpio_init(GPIOA,&gpio_struct);//PA
   ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_12, LL_GPIO_AF_0);
 
    SYSCTRL_REG_OPT(
        SYSCTRL->SYS_CON0 = (SYSCTRL->SYS_CON0 & (~LL_SYSCTRL_CON0_SLEEP_DLY_CNT(ALL_WORD_FF))) 
                                    | LL_SYSCTRL_CON0_SLEEP_DLY_CNT(0x5);
        SYSCTRL->WKUP_CON0 = 0xF << 24;
        SYSCTRL->WKUP_CON0 |= 0x1 << 1;
    );
}

void gpio_wakeup_sleep_mode(void)
{
    //gpio
    GPIOA->MODE = 0x0;                      //PA in
    GPIOB->MODE = 0x0;                      //PB in
 
    GPIOA->PUPD = ((u32)0xFFFF << 16);
    GPIOB->PUPD = ((u32)0x3FFF << 16);

    SYSCTRL_REG_OPT(
        SYSCTRL->WKUP_CON0 = (0xF << 24);
        SYSCTRL->RPCON = (0x1 << 16);
    );    
    wk_up_io_fun_set();

    //clk
    SYSCTRL_REG_OPT(
        SYSCTRL->LP_CON0 |= (0x1 << 5);         //RC256K_SOFT_EN
        SYSCTRL->CLK_CON0 &= ~(0x3 << 0);       //sys clk select 256k  
        SYSCTRL->LP_CON0 |= (0x1 << 4);         //RC256K_AUTO_DIS
        SYSCTRL->LP_CON0 |= (0x1 << 7);

        SYSCTRL->CLK_CON3 &= ~(0x1 << 17);      //disable HIRC_EN 
        
        SYSCTRL->SYS_CON0 |= (0x1 << 30);       //after wake up no reset

        //pmu
        SYSCTRL->PMU_CON0 &= ~(0x1 << 12);      //INNER_LDO_EN
        SYSCTRL->PMU_CON0 = (SYSCTRL->PMU_CON0 & ~(0x7 << 0)) | (0x3 << 0); //PMU_LDOS select 1.6v
        SYSCTRL->PMU_CON0 = (SYSCTRL->PMU_CON0 & ~(0x7 << 4)) | (0x2 << 4); //PMU_LLDOS select 1.6v
        SYSCTRL->PMU_CON0 &= ~(0x1 << 18);      //HP_PDLI_EN
        SYSCTRL->PMU_CON0 &= ~(0x1 << 17);      //HP_PDI_EN
        SYSCTRL->PMU_CON0 &= ~(0x1 << 20);      //HP_V2I_EN
        SYSCTRL->PMUBK &= ~(0x7 << 4);

        SYSCTRL->LP_CON0 |= (0x1 << 0);         //enter sleep mode

        SYSCTRL->PMU_CON0 |= (0x1 << 13);       //HP_LDO_EN
        SYSCTRL->PMU_CON0 |= (0x1 << 20);       //HP_V2I_EN
        SYSCTRL->PMU_CON0 |= (0x1 << 18);       //HP_PDLI_EN
        SYSCTRL->PMU_CON0 |= (0x1 << 17);       //HP_PDI_EN
        SYSCTRL->PMU_CON0 |= (0x1 << 12);       //INNER_LDO_EN
        
        SYSCTRL->PMU_CON0 &= ~(0x7 << 0);
        SYSCTRL->PMU_CON0 &= ~(0x7 << 4);

        SYSCTRL->RPCON = (0x1 << 16);
        
        SYSCTRL->CLK_CON3 |= (0x1 << 17);       //enable HIRC_EN
        SYSCTRL->CLK_CON3 |= (0x3 << 10);       //48M 
        while(!(SYSCTRL->CLK_CON3 & ((u32)0x1 << 31)));
        SYSCTRL->CLK_CON0 |= (0x2 << 0);        //sys clk select HIRC
        
        GPIOA->MODE = (GPIOA->MODE & ~(0x3 << 26)) | (0x1 << 26);
        SYSCTRL->CLK_CON0 = SYSCTRL->CLK_CON0 & ~(0x3 << 2);            //clk to IO sys 
        SYSCTRL->CLK_CON1 = (SYSCTRL->CLK_CON1 & ~(((u32)0x1F << 27) ))
                                    | ((u32)3 << 27) ;                  //clk to IO div 4
                                    
        SYSCTRL->SYS_CON1 |= (0x1 << 6);                                //clk out to PA13
    );
}

void gpio_wakeup_handle(void)
{
    u32 tick;
    
    TYPE_LL_GPIO_INIT gpio_struct;
    memset(&gpio_struct,0x00,sizeof(gpio_struct));
    gpio_struct.gpio_pin   = LL_GPIO_PIN_5|LL_GPIO_PIN_4;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_OUT;
    ll_gpio_init(GPIOB, &gpio_struct);
    ll_gpio_set_bits(GPIOB, LL_GPIO_PIN_5);  
    ll_gpio_reset_bits(GPIOB, LL_GPIO_PIN_4);  
    tick = GetSysTick();
    while(!is_systick_expired(tick, 4000));
    
    gpio_wakeup_sleep_mode();


    memset(&gpio_struct,0x00,sizeof(gpio_struct));
    gpio_struct.gpio_pin   = LL_GPIO_PIN_5|LL_GPIO_PIN_4;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_OUT;
    ll_gpio_init(GPIOB, &gpio_struct);
    ll_gpio_set_bits(GPIOB, LL_GPIO_PIN_5);  
    ll_gpio_reset_bits(GPIOB, LL_GPIO_PIN_4);  
    while(1);
}

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2021 JUSHENG ***** END OF FILE *****/
